import Vue from "vue";
import AppActionSheet from "./app-action-sheet.vue";
import { Subject } from 'rxjs';

export default class ActionSheet{

    /**
     * 实例对象
     *
     * @private
     * @static
     * @memberof ActionSheet
     */
    private static actionSheet:any;
    
    /**
     * vue 实例
     *
     * @private
     * @type {Vue}
     * @memberof ActionSheet
     */
    private vm!: Vue;


    /**
     * 单例构造函数
     * 
     * @memberof ActionSheet
     */
    private constructor() {
        if (ActionSheet.actionSheet) {
            return ActionSheet.actionSheet;
        }
    }
    
    /**
     * 静态获取单例对象方法
     *
     * @static
     * @returns {ActionSheet}
     * @memberof ActionSheet
     */
    public static getInstance(): ActionSheet {
        if (!ActionSheet.actionSheet) {
            ActionSheet.actionSheet = new ActionSheet();
        }
        return ActionSheet.actionSheet;
    }

    /**
     * 打开上拉菜单
     *
     * @param {actions: Array<any>} prop 选项组
     * @returns {Subject<any>}
     * @memberof ActionSheet
     */
    public openSheet(props: { actions: Array<any>}): Subject<any> {
        try {
            let component = AppActionSheet;
            const vm = new Vue({
                render(h) {
                    return h(component, { props });
                }
            }).$mount();
            this.vm = vm;
            document.body.appendChild(vm.$el);
            const comp:any = vm.$children[0];
            return comp.show();
        } catch (error) {
            console.log(error);
            return new Subject<any>();
        }
    }

}